package Demo1;

class Animal {
    public void display() {
        System.out.println("Animal");
    }
}

class Dog extends Animal {
    public void display() {
        System.out.println("dog");
    }

    public void eat() {
        System.out.println("吃骨头");
    }
}
public class Test_4 {
    public static void main(String[] args) {
        //向上转型
        Animal animal = new Dog();
        
        //判断instanceof 是否为 true
        if(animal instanceof Dog) {
            //向下转型
            animal = (Dog)animal;
            ((Dog) animal).eat();
        } else {
            System.out.println("Animal无法向下转型为Dog");
        }
    }
}
